package com.example.project.Controller;

import com.example.project.Entity.Code;
import com.example.project.Entity.User;
import com.example.project.Service.CodeService;
import com.example.project.Service.MailService;
import com.example.project.Service.PhoneService;
import com.example.project.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private MailService mailService;
    @Autowired
    private CodeService codeService;
    @Autowired
    private PhoneService phoneService;

    @PostMapping("/check")
    public Map<String, Object> GetCode(@RequestBody Map<String, String> ch_map){
        String type = ch_map.get("type");
        String mail = ch_map.get("e_mail");
        String phone = ch_map.get("phone_number");
        Map<String, Object> map = new HashMap<>();
        try{
            if(type.equals("e_mail")){
                User user1 = userService.selectUserByMail(mail);
                if (user1 != null) {
                    map.put("errno", "1002");
                    map.put("message", "该邮箱已注册！");
                    return map;
                }
                int flag = mailService.sendMailCode(mail);
                if(flag==0){
                    map.put("errno", "1001");
                    map.put("message", "邮箱不存在！");
                }
                else {
                    map.put("errno", "1");
                    map.put("message", "验证码发送成功，请前往邮箱查看！");
                }
            }
            if(type.equals("phone_number")){
                User user1 = userService.selectUserByPhone(phone);
                if (user1 != null) {
                    map.put("errno", "1004");
                    map.put("message", "该手机号已注册！");
                    return map;
                }
                int flag = phoneService.sendPhoneCode(phone);
                if(flag==0){
                    map.put("errno", "1003");
                    map.put("message", "手机号不存在！");
                }
                else {
                    map.put("errno", "2");
                    map.put("message", "验证码发送成功，请前往手机短信查看！");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("errno", "1005");
            map.put("message", "验证码发送失败！");
        }
        return map;
    }

    @PostMapping("/register")
    public Map<String, Object> Register(@RequestBody Map<String, String> re_map){
        String type = re_map.get("type");
        String e_mail = re_map.get("e_mail");
        String phone_number = re_map.get("phone_number");
        String username = re_map.get("username");
        String password1 = re_map.get("password1");
        String password2 = re_map.get("password2");
        String code = re_map.get("code");
        Map<String, Object> map = new HashMap<>();
        try {
            if(type.equals("e_mail")){
                Code code1 = codeService.selectCode(e_mail, code);
                if(code1==null){
                    map.put("errno", "2001");
                    map.put("message", "验证码不正确！");
                    return map ;
                }
            }
            if(type.equals("phone_number")){
                Code code1 = codeService.selectCode(phone_number, code);
                if(code1==null){
                    map.put("errno", "2001");
                    map.put("message", "验证码不正确！");
                    return map ;
                }
            }
            if(username.length()<4 || username.length()>20){
                map.put("errno", "2002");
                map.put("message", "用户名的长度应为4~20位");
                return map ;
            }
            if(!username.matches("^[a-z0-9A-Z_]+$")){
                map.put("errno", "2003");
                map.put("message", "用户名中只能由字母、数字和下划线组成！");
                return map ;
            }
            User user1 = userService.selectUserByUsername(username);
            if (user1 != null) {
                map.put("errno", "2004");
                map.put("message", "用户名已注册！");
                return map;
            }
            if(password1.length()<8 || password1.length()>24){
                map.put("errno", "2005");
                map.put("message", "密码的长度为8~24位！");
                return map;
            }
            if((!password1.matches("^[a-z0-9A-Z]+$"))
                    || password1.matches("^[a-z]+$")
                    || password1.matches("^[0-9]+$")
                    || password1.matches("^[A-Z]+$")){
                map.put("errno", "2006");
                map.put("message", "密码需要同时包含大小写字母和数字！");
                return map;
            }
            if (!password1.equals(password2)){
                map.put("errno", "2007");
                map.put("message", "两次输入的密码不一致！");
                return map;
            }
            User user = new User(username, password1, e_mail, null, phone_number, null);
            userService.registerNewUser(user);
            map.put("errno", "1");
            map.put("message", "用户注册成功！");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("errno", "2008");
            map.put("message", "用户注册失败！");
        }
        return map;
    }

    @PostMapping("/login")
    public Map<String, Object> Login(@RequestBody Map<String, String> lo_map) {
        String type = lo_map.get("type");
        String e_mail = lo_map.get("e_mail");
        String phone_number = lo_map.get("phone_number");
        String username = lo_map.get("username");
        String password = lo_map.get("password");
        Map<String, Object> map = new HashMap<>();
        try{
            User user1;
            if(type.equals("username")){
                user1 = userService.selectUserByUsername(username);
            } else if(type.equals("e_mail")){
                user1 = userService.selectUserByMail(e_mail);
            } else {
                user1 = userService.selectUserByPhone(phone_number);
            }
            if(user1 == null){
                map.put("errno", "3001");
                map.put("message", "用户名不存在！");
                return map;
            }
            if (!user1.getPassword().equals(password)) {
                map.put("errno", "3002");
                map.put("message", "密码错误！");
            }
            map.put("errno", "1");
            map.put("message", "用户登录成功！");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("errno", "3003");
            map.put("message", "用户登录失败！");
        }
        return map ;
    }

    @PostMapping("/modify")
    public Map<String, Object> Modify(@RequestBody Map<String, String> mo_map){
        String username = mo_map.get("username");
        String e_mail = mo_map.get("e_mail");
        String phone_number = mo_map.get("phone_number");
        String code1 = mo_map.get("code1");
        String code2 = mo_map.get("code2");
        String avatar = mo_map.get("avatar");
        String sex = mo_map.get("sex");

        Map<String, Object> map = new HashMap<>();
        try{
            User user1 = userService.selectUserByUsername(username);
            if(!user1.getE_mail().equals(e_mail)){
                Code code3 = codeService.selectCode(e_mail,code1);
                if(code3==null) {
                    map.put("errno", "18001");
                    map.put("message", "邮箱验证码不正确！");
                    return map;
                }
                user1.setE_mail(e_mail);
            }
            if(!user1.getPhone_number().equals(phone_number)){
                Code code3 = codeService.selectCode(e_mail,code2);
                if(code3==null) {
                    map.put("errno", "18002");
                    map.put("message", "手机号验证码不正确！");
                    return map;
                }
                user1.setPhone_number(phone_number);
            }
            if(!user1.getSex().equals(sex)){
                user1.setSex(sex);
            }
            if(!user1.getAvatar().equals(avatar)){
                user1.setAvatar(avatar);
            }
            userService.deleteUserByUsername(username);
            userService.registerNewUser(user1);
            map.put("errno", "1");
            map.put("message", "修改信息成功！");
        } catch (Exception e){
            map.put("errno", "18003");
            map.put("message", "修改信息失败！");
        }
        return map;
    }

    @PostMapping("/changePassword")
    public Map<String, Object> ChangePassword(@RequestBody Map<String, String> ch_map){
        String username = ch_map.get("username");
        String old_password = ch_map.get("old_password");
        String new_password1 = ch_map.get("new_password1");
        String new_password2 = ch_map.get("new_password2");
        Map<String, Object> map = new HashMap<>();
        try{
            User user1 = userService.selectUserByUsername(username);
            if(!user1.getPassword().equals(old_password)){
                map.put("errno", "19001");
                map.put("message", "原密码错误！");
                return map;
            }
            if(new_password1.length()<8 || new_password1.length()>24){
                map.put("errno", "19002");
                map.put("message", "新密码的长度为8~24位！");
                return map;
            }
            if((!new_password1.matches("^[a-z0-9A-Z]+$"))
                    || new_password1.matches("^[a-z]+$")
                    || new_password1.matches("^[0-9]+$")
                    || new_password1.matches("^[A-Z]+$")){
                map.put("errno", "19003");
                map.put("message", "新密码需要同时包含大小写字母和数字！");
                return map;
            }
            if (!new_password1.equals(new_password2)){
                map.put("errno", "19004");
                map.put("message", "两次输入的新密码不一致！");
                return map;
            }
            user1.setPassword(new_password1);
            userService.deleteUserByUsername(username);
            userService.registerNewUser(user1);
            map.put("errno", "1");
            map.put("message", "修改密码成功！");
        } catch (Exception e){
            map.put("errno", "19005");
            map.put("message", "修改密码失败！");
        }
        return map;
    }
}
